# -*- coding: utf-8 -*-
from core import app
from auth import auth, login as auth_login, logout as auth_logout
from flask import redirect, url_for, render_template, g, request, jsonify
from models import Question, Survey, Option

@app.route('/')
@app.route('/home')
def home():
    return render_template('home.html')


@app.route('/create', methods=['GET', 'POST'])
def create():
    if request.method == 'GET':
        question_number = request.args.get('question_number') or 10
        option_number = request.args.get('option_number') or 4
        return render_template('create.html', question_number=question_number, option_number=option_number)
    else:
        survey = Survey()
        survey.title = request.form['title']
    
        i = 1
        question_content = request.form.get('question' + str(i))
        while question_content:
            j = 1
            question = Question()
            question.content = question_content
            correct_option = request.form.get('option' + str(i) + '_check')
            option_content = request.form.get('option' + str(i) + '_' + str(j))
            while option_content:
                option = Option()
                option.content = option_content
                if j == correct_option:
                    option.correctness = True
                else:
                    option.correctness = False
                question.options.append(option)
                j = j + 1
                option_content = request.form.get('option' + str(i) + '_' + str(j))
            survey.questions.append(question)
            i = i + 1
            question_content = request.form.get('question' + str(i))
        survey.question_count = i - 1
        return 'created suc!'
                    
